<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Cache;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class IndexShowController extends Controller
{
    
    //一级板块
    public function getIndex()
    {

      //获取分类板块
        $type = DB::table('type')->where('pid','0')->get();
        $datas = self::pushCates(self::pushType(),0);
        $ptype = DB::table('ptype')->where('pid','0')->first();
       //获取配置信息
        $config = DB::table('config')->first();
        $pts = DB::table('ptype')->where('pid','!=','0')->take(6)->get();
       //友情链接
       $blinks = DB::table('blink')->get();
       Cache::forget('head');
       $res = Cache::add('head', ['type'=>$type,'config'=>$config,'blinks'=>$blinks,'datas'=>$datas,'ptype'=>$ptype,'pts'=>$pts], 1440);
       // var_dump($type);
       return view('Home.layout.layout',['type'=>$type,'config'=>$config,'blinks'=>$blinks,'datas'=>$datas,'ptype'=>$ptype,'pts'=>$pts]);
      
    }

    //获取全部一级分类板块
    public static function pushType()
    {
        return $cates = DB::table('type')->get();


    }

  
      //获取全部以及分类
       $type = DB::table('type')->where('pid',0)->get();
      // dd($type);

      //获取配置信息
       $config = DB::table('config')->first();

      //友情链接
       $blinks = DB::table('blink')->get();

      //遍历下部的栏目
      $datas = self::pushCates(self::pushType(),0);

      // var_dump($type);
       return view('Home.layout.layout',['type'=>$type,'config'=>$config,'blinks'=>$blinks,'datas'=>$datas]);
      
    }


     //获取全部一级分类板块
    public static function pushType()
    {
        return $cates = DB::table('type')->get();
    }

    //获取二级分类
    public  static function pushCates($cates,$pid)
    {
        $data = [];
        foreach($cates as $k=>$v)
        {
            if($v->pid == $pid){

               $v->sub = self::pushCates($cates,$v->id);
               $data[] = $v;
            }
        }
        return $data;
    }

}
